<?php

/**
 * 响应数据
 */
class Result
{
    /**
     * @var object 响应数据
     */
    public $data;

    /**
     * @var int 数据总量
     */
    public $total;
    // 当前页
    public $cur;
    // 提示信息
    public $message;
    // 状态码 1表示处理成功，0表示处理失败
    public $code;

    /**
     * @param $data
     * @param int $total
     */
    public function __construct($data, int $total)
    {
        $this->data = $data;
        $this->total = $total;
        $this->code = 1;
    }

    public static function page($data, int $total, int $cur): Result
    {
        $result = new Result($data, $total);
        $result->cur = $cur;
        return $result;
    }


    public static function empty(): Result
    {
        return new Result(null, 0);
    }

    public static function success($data): Result
    {
        return new Result($data, 0);
    }

    public static function error(string $msg): Result
    {
        $empty = self::empty();
        $empty->message = $msg;
        $empty->code = 0;
        return $empty;
    }
}